package com.qms.repast.utils;

import android.graphics.Bitmap;
import android.os.Environment;

import com.orhanobut.logger.Logger;
import com.qms.repast.App;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author 泅渡者
 * @date 2018/6/6 0006.
 * @describe:
 */

public class FileUtil {
	/**
	 * 获取文件夹
	 */
	public static final String SDPATH = App.getInstance().getExternalCacheDir().getAbsolutePath() + "files/";

	public static void init () {
		createDir(SDPATH);
	}

	public static File createDir (String path) {
		File file = new File(path);
		if (!file.exists()) {
			//mkdirs可以创建多级目录下的文件夹
			file.mkdirs();
		}
		return file;

	}

	/**
	 * 保持图片
	 *
	 * @param mBitmap
	 * @param path
	 * @param imgName
	 *
	 * @return
	 */
	public static boolean saveBitmap (Bitmap mBitmap, String path, String imgName) {
		String sdStatus = Environment.getExternalStorageState();
		// 检测sd是否可用
		if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
			return false;
		}
		FileOutputStream b = null;
		File file = new File(path);
		// 创建文件夹
		file.mkdirs();
		String fileName = path + imgName;
		//删除本地旧图
		delFile(path, imgName);
		try {
			b = new FileOutputStream(fileName);
			// 把数据写入文件
			mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				b.flush();
				b.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	/**
	 * 保存图片到SD卡
	 *
	 * @param is
	 * @param path
	 * @param imgName
	 *
	 * @return
	 */
	public static boolean saveImgToSdCard (InputStream is, String path, String imgName) {
		String sdStatus = Environment.getExternalStorageState();
		// 检测sd是否可用
		if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
			return false;
		}
		createDir(path);
		//删除本地旧图
		delFile(path, imgName);
		try {
			File file = new File(path, imgName);
			FileOutputStream fos = new FileOutputStream(file);
			BufferedInputStream bis = new BufferedInputStream(is);
			byte[] buffer = new byte[1024];
			int len;
			while ((len = bis.read(buffer)) != -1) {
				fos.write(buffer, 0, len);
				fos.flush();
			}
			fos.close();
			bis.close();
			is.close();
			Logger.d("下载图片成功" + path + imgName);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			Logger.e(e.toString());
			return false;
		}
	}

	/**
	 * 创建目录
	 *
	 * @param dirName
	 *
	 * @return
	 *
	 * @throws IOException
	 */
	public static File createSDDir (String dirName) throws IOException {
		File dir = new File(SDPATH + dirName);
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			System.out.println("createSDDir:" + dir.getAbsolutePath());
			System.out.println("createSDDir:" + dir.mkdir());
		}
		return dir;
	}

	/**
	 * 判断文件是否存在
	 *
	 * @param fileName
	 *
	 * @return
	 */
	public static boolean isFileExist (String fileName) {
		File file = new File(SDPATH + fileName);
		file.isFile();
		return file.exists();
	}

	/**
	 * 删除文件
	 *
	 * @param path
	 * @param fileName
	 */
	public static void delFile (String path, String fileName) {
		File file = new File(path + fileName);
		if (file.isFile()) {
			file.delete();
		}
		file.exists();
	}

	/**
	 * 删除文件夹和文件夹里面的文件
	 *
	 * @param pPath
	 */
	public static void deleteDir (final String pPath) {
		File dir = new File(pPath);
		deleteDirWihtFile(dir);
	}

	/**
	 * 删除所有
	 *
	 * @param dir
	 */
	public static void deleteDirWihtFile (File dir) {
		if (dir == null || !dir.exists() || !dir.isDirectory()) {
			return;
		}
		for (File file : dir.listFiles()) {
			if (file.isFile()) {
				// 删除所有文件
				file.delete();
			} else if (file.isDirectory()) {
				// 递规的方式删除文件夹
				deleteDirWihtFile(file);
			}
		}
		// 删除目录本身
		dir.delete();
	}

	/**
	 * 判断文件是否存在
	 *
	 * @param path
	 *
	 * @return
	 */
	public static boolean fileIsExists (String path) {
		try {
			File f = new File(path);
			if (!f.exists()) {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}
